package httpUrlTest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.util.AntPathMatcher;
import org.springframework.util.PathMatcher;

public class AntMatchUtil {

    private static final PathMatcher pathMatcher = new AntPathMatcher();


    /**
     * @author: zhipingzhang
     * @date: 2018/11/16 15:20
     * @description: 校验是否满足path规则
     */
    public static boolean validatePattern(String patternPath){
        if(StringUtils.isBlank(patternPath)){
            return false;
        }
        patternPath = path(patternPath);
        if(!pathMatcher.isPattern(patternPath)){
            return false;
        }
        if(!patternPath.contains("**")){
            return true;
        }
        //不支持url参数
        if(patternPath.contains("{") && patternPath.contains("}")){
            return false;
            //patternPath = patternPath.substring(0, patternPath.indexOf("{")-1);
        }
        if(patternPath.split("\\*\\*").length>1){
            return false;
        }
        if(patternPath.endsWith("**")){
            return true;
        }
        return false;
    }

    public static String concatPath(String ... paths){
        StringBuilder pathBuild = new StringBuilder();
        for(String path : paths){
            pathBuild.append(path(path));
        }
        return pathBuild.toString();
    }

    /**
     * @author: zhipingzhang
     * @date: 2018/11/16 14:20
     * @description:返回null证明不匹配
     * 最复杂的情况下支持的匹配串请看AntMatchTestTest的junit case
     *
     */
    public static String replacePathWithinPattern(String patternPath, String remotePath, String targetPath) {
        patternPath = path(patternPath);
        remotePath = path(remotePath);
        if (!pathMatcher.match(patternPath, remotePath)) {
            return null;
        }
        //未自定义转发路径或
        if (StringUtils.isBlank(targetPath)) {
            return remotePath;
        }

        targetPath = path(targetPath);
        //包含替换占位符 仅替换Url参数即可
        if (targetPath.contains("#{")) {

            String extractPath = pathMatcher.extractPathWithinPattern(patternPath, remotePath);
            extractPath = path(extractPath);
            int i = 1;
            while (targetPath.contains("#{" + i + "}")) {
                String mathPath = extractPath.split("/")[1];
                targetPath = targetPath.replace("#{" + i + "}", mathPath);
                extractPath = extractPath.substring(mathPath.length()+1, extractPath.length());
                i++;
            }
            if (patternPath.endsWith("**")) {
                targetPath = concatPath(targetPath, extractPath);
            }

        }
        return targetPath;
    }

    /**
     * @author: zhipingzhang
     * @date: 2018/11/16 14:55
     * @description: 转成前面有/  后面没/的
     */
    public static String path(String path){
        if(StringUtils.isBlank(path)) {
            path = "";
        }else if (path.startsWith("/")) {

        }else{
            path = "/" + path;
        }
        if(path.endsWith("/")){
            path = StringUtils.substringBeforeLast(path, "/");
        }
        return path;
    }
}
